﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetFrameworkExtensions.Enterprise.Security
{
  public abstract class AuthorizableOperation<TOperation> :
    IAuthorizableOperation
    where TOperation : AuthorizableOperation<TOperation>
  {
    internal AuthorizableOperation(string name, AuthorizableOperationKind kind)
    {
      if (string.IsNullOrWhiteSpace(name))
        throw new ArgumentNullException("name");
      if (!Enum.IsDefined(typeof(AuthorizableOperationKind), kind))
        throw new ArgumentOutOfRangeException("kind");

      this.Name = name;
      this.Kind = kind;
    }

    public string Name { get; private set; }

    public AuthorizableOperationKind Kind { get; private set; }

    public sealed override bool Equals(object obj)
    {
      if (obj == null) return false;
      if (GetType() != obj.GetType()) return false;
      return EqualsOverride((TOperation)obj);
    }

    protected virtual bool EqualsOverride(TOperation other)
    {
      return other.Name == Name && other.Kind == Kind;
    }

    public override int GetHashCode()
    {
      unchecked
      {
        return Name.GetHashCode() * 37 ^ Kind.GetHashCode();
      }
    }

    public abstract override string ToString();

    public abstract string EntityName { get; }
  }
}
